package com.oocl.client.process.impl;

import java.io.IOException;
import java.io.ObjectOutputStream;

import com.oocl.client.callback.ProtocolSendCallback;
import com.oocl.client.process.ProtocolSender;
import com.oocl.client.util.JacksonUtil;
import com.oocl.qq.protocol.Protocol;

public class ProtocolSenderImpl implements ProtocolSender{

	private ObjectOutputStream objectOut;
	private ProtocolSendCallback mCallback;
	private boolean canSend = false;
	private Protocol p;
	
	public  ProtocolSenderImpl(ProtocolSendCallback callback) {
			this.mCallback = callback;
	}
	
	
	@Override
	public void sendProtocol(Protocol p) {
		this.p = p;
		if(this.mCallback!=null)
			this.mCallback.startSend();
	}


	@Override
	public void canSendProtocol(ObjectOutputStream objectOut) {
		this.objectOut = objectOut;
		if(this.objectOut!=null)
			this.canSend = true;
		else
			this.canSend = false;
		if(this.canSend) {
			try {
				if(this.objectOut!=null)
//					this.objectOut.writeObject(this.p);
//					this.objectOut.writeObject(null);
					
					this.objectOut.writeObject(JacksonUtil.fromObjectToJson(this.p));
					
				}catch(IOException e) {
					e.printStackTrace();
				}finally {
					if(this.mCallback!=null)
					this.mCallback.finishSend();
					this.canSend = false;
				}
		}
	}
	
}
